package com.cskaoyan.gateway.controller.user;


import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.shopping.dto.CartListByIdRequest;
import com.mall.shopping.dto.CartListByIdResponse;
import com.mall.user.IKaptchaService;
import com.mall.user.RegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserVerifyRequest;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Anoymous
public class RegisterController {
    @Reference(timeout = 1200000 ,check = false)
    RegisterService registerService;

    @Reference(check = false)
    IKaptchaService iKaptchaService;

    @Reference(check = false)
    IKaptchaService kaptchaService;


    /**
     * 用户注册
     * @param registerRequest
     * @param httpServletRequest
     * @return ResponseData
     */
    @RequestMapping("/register")
    public ResponseData register(@RequestBody UserRegisterRequest registerRequest, HttpServletRequest httpServletRequest){
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(registerRequest.getCaptcha());

        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            boolean register = registerService.register(registerRequest);

            if (register){
                return new ResponseUtil<>().setData(null);
            }else{
                return new ResponseUtil<>().setErrorMsg("用户名或邮箱已存在");
            }

        }else{
            return new ResponseUtil<>().setErrorMsg(003005,"验证码错误！");
        }
    }


    /**
     * 用户验证
     * @param verifyRequest
     * @return ResponseData
     */
    @GetMapping("/verify")
    public ResponseData verify(UserVerifyRequest verifyRequest){
        boolean isVerify = registerService.verify(verifyRequest);
        if(isVerify){
            return new ResponseUtil<>().setData("验证成功！！！");
        }else {
            return new ResponseUtil<>().setErrorMsg(003200,"");
        }

    }
}
